/*
 * To change this template, chopromotione Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.PromotionDao;
import com.techshop.model.Promotion;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class PromotionDaoImpl extends HibernateDaoSupport implements PromotionDao {

    @Override
    public Promotion getById(int promotionId) {
        try {
            return (Promotion) getSession().get(Promotion.class, promotionId);
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Promotion> getAll() {
        try {
            return getSession().createQuery("FROM Promotion").list();
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Promotion promotion) {
        try {
            getHibernateTemplate().save(promotion);
            ValidatePhonePubSubClient.changePromotion();
            return true;
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean update(Promotion promotion) {
        try {
            if (getById(promotion.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().update(getHibernateTemplate().merge(promotion));
                ValidatePhonePubSubClient.changePromotion();
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Promotion promotion) {
        try {
            if (getById(promotion.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().delete(getHibernateTemplate().merge(promotion));
                ValidatePhonePubSubClient.changePromotion();
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}